#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
typedef double db;
inline int read()
{
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9') { if(ch=='-')f=-1; ch=getchar(); }
	while(ch>='0'&&ch<='9') { x=(x<<1)+(x<<3)+ch-'0'; ch=getchar(); }
	return x*f;
}
const int MAXN=1e5+10,MOD=1e9+7;
int n,m,root,tot,mi[MAXN];
char s[MAXN];
struct Splay
{
	int c[2],fa,val,siz,hash;
}a[MAXN];
void push_up(int o)
{
	a[o].siz=a[a[o].c[0]].siz+a[a[o].c[1]].siz+1;
	a[o].hash=(((LL)a[a[o].c[0]].hash*mi[a[a[o].c[1]].siz+1]%MOD+(LL)a[o].val*mi[a[a[o].c[1]].siz]%MOD)%MOD+a[a[o].c[1]].hash)%MOD;
	return;
}
void rotate(int &o,int x)
{
	int y=a[x].fa,z=a[y].fa,dy=a[y].c[1]==x,dz=a[z].c[1]==y;
	if(o==y)o=x;
	else a[z].c[dz]=x;
	a[x].fa=z;
	a[y].c[dy]=a[x].c[dy^1];
	a[a[y].c[dy]].fa=y;
	a[x].c[dy^1]=y;
	a[y].fa=x;
	push_up(y);
	return;
}
void splay(int &o,int x)
{
	while(o!=x)
	{
		int y=a[x].fa,z=a[y].fa;
		if(o!=y)
		{
			if((a[y].c[1]==x)^(a[z].c[1]==y))rotate(o,x);
			else rotate(o,y);
		}
		rotate(o,x);
	}
	push_up(x);
	return;
}
int find(int o,int v)
{
	if(v<=a[a[o].c[0]].siz)return find(a[o].c[0],v);
	if(v<=a[a[o].c[0]].siz+1)return o;
	return find(a[o].c[1],v-a[a[o].c[0]].siz-1);
}
int main()
{
	scanf("%s",s+1);
	n=strlen(s+1);
	m=read();
	mi[0]=1;
	for(int i=1;i<=100000;++i)mi[i]=(LL)mi[i-1]*27%MOD;
	a[1].siz=n+2;a[1].c[1]=2;
	a[n+2].fa=n+1;a[n+2].siz=1;
	for(int i=n;i;--i)a[i+1]=(Splay){{0,i+2},i,s[i]-'a',n+2-i,0},push_up(i+1);
	tot=n+2;root=1;
	while(m--)
	{
		scanf("%s",s);
		if(s[0]=='Q')
		{
			int x=read(),y=read();
			if(x>y)swap(x,y);
			int l=0,r=n+2-y;
			while(l<r-1)
			{
				int mid=(l+r)>>1;
				splay(root,find(root,x));
				splay(a[root].c[1],find(root,x+mid+1));
				int tmp=a[a[a[root].c[1]].c[0]].hash;
				splay(root,find(root,y));
				splay(a[root].c[1],find(root,y+mid+1));
				tmp-=a[a[a[root].c[1]].c[0]].hash;
				tmp?r=mid:l=mid;
			}
			printf("%d\n",l);
		}
		else if(s[0]=='R')
		{
			int x=read();
			scanf("%s",s);
			splay(root,find(root,x+1));
			a[root].val=s[0]-'a';
			push_up(root);
		}
		else
		{
			int x=read();
			scanf("%s",s);
			++n;
			splay(root,find(root,x+1));
			splay(a[root].c[1],find(root,x+2));
			a[a[root].c[1]].c[0]=++tot;
			a[tot]=(Splay){{0,0},a[root].c[1],s[0]-'a',1,s[0]-'a'};
			push_up(a[root].c[1]);
			push_up(root);
		}
	}
	return 0;
}
